#pragma once

#include <iostream>
#include <pthread.h>
#include <mutex>

class Mutex
{
public:
    Mutex(pthread_mutex_t* mtx)
        :_mtx(mtx)
    {}

    void lock()
    {
        pthread_mutex_lock(_mtx);
    }

    void unlock()
    {
        pthread_mutex_unlock(_mtx);
    }

    ~Mutex()
    {}
private:
    pthread_mutex_t* _mtx;
};

// RAII风格的锁, 构造的时候上锁, 析构的时候解锁
class lockGuard
{
public:
    lockGuard(pthread_mutex_t * mtx)
        :_mtx(mtx)
    {
        std::cout << "进行加锁 " << std::endl;
        _mtx.lock();
    }

    ~lockGuard()
    {
        std::cout << "进行解锁 " << std::endl;
        _mtx.unlock();
    }
private:
    Mutex _mtx;
};
